/*
 * @Descripttion: 
 * @version: 
 * @Author: MichaelHu
 * @Date: 2022-01-07 13:59:41
 * @LastEditors: MichaelHu
 * @LastEditTime: 2022-01-13 15:04:10
 */
#include "FreeRTOS.h"
#include "lauxlib.h"
#include "lua.h"
#include "lua_obj.h"
#include "lvgl/lvgl.h"
#include "main.h"
#include "task.h"

static int delay_ms(lua_State *L)
{
    uint16_t n = luaL_checkinteger(L, 1);
    vTaskDelay(n);
    return 1;
}

static int restart(lua_State *L)
{
    NVIC_SystemReset();
    return 1;
}

static int lvgl_mem_size(lua_State *L)
{
    lv_mem_monitor_t mem_monitor;
    lv_mem_monitor(&mem_monitor);
    lua_pushinteger(L, mem_monitor.total_size);
    lua_pushinteger(L, mem_monitor.free_size);
    return 2;
}

static int help(lua_State *L);
static const struct luaL_Reg lv_obj_Lib[] = {
    {"delay_ms", delay_ms},
    {"restart", restart},
    {"lvgl_mem_size", lvgl_mem_size},

    {"help", help},
    {NULL, NULL}};

static int help(lua_State *L)
{
    uint16_t n = sizeof(lv_obj_Lib) / sizeof(struct luaL_Reg);

    for (int i = 0; i < n - 1; i++)
    {
        printf("%s\r\n", lv_obj_Lib[i].name);
    }
    return 1;
}

LUALIB_API int lua_user_funinit(lua_State *L)
{
    luaL_newlib(L, lv_obj_Lib);
    return 1;
}
